package com.tms.app.tms.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tms.app.tms.R;
import com.tms.app.tms.adapter.GoodsInfoAdapter;
import com.tms.app.tms.bean.GoodsInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.bean.response.GoodsRes;
import com.tms.app.tms.ui.base.ui.BaseListActivity;
import com.tms.app.tms.utils.SharePreferenceUtil;
import com.tms.app.tms.widget.InputDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class GoodsInfoActivity extends BaseListActivity implements BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.button_send)
    Button buttonSend;
    private int dispatchId = 0;
    private  boolean isEdited=false;
    GoodsPresenter presenter;
    private InputDialog cdialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_goods);
        ButterKnife.bind(this);
        setToolBarTitle("件毛体详情", -1, -1);
        dispatchId = getIntent().getIntExtra("dispatchId", 0);
        token= SharePreferenceUtil.getToken(this);
        presenter = new GoodsPresenter();
        presenter.setiView(this);
        initView();
        presenter.getGoodsList(token, dispatchId);

    }
    @OnClick(R.id.button_send)
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.button_send:
                Intent intent=new Intent(this, AddGoodsActivity.class);
                //intent.putExtra("goodInfo",gson.toJson(goodsInfo));
                intent.putExtra("dispatchId",dispatchId);
                startActivityForResult(intent,1);
                if (true)
                    return;
                if (adapter.getData().size()>0)
                {
                    GoodsRes goodsRes=new GoodsRes();
                    goodsRes.setToken(token);
                    goodsRes.setData(adapter.getData());
                    presenter.updateGoodsList(goodsRes);
                }
                break;
            case R.id.dialog_photo_button_2:
                adapter.notifyDataSetChanged();
                cdialog.close();
                break;

        }
    }

    public void initView() {
        swipeLayout.setColorSchemeColors(Color.rgb(47, 223, 189));
        swipeLayout.setOnRefreshListener(presenter);
        adapter = new GoodsInfoAdapter();
        adapter.setOnItemChildClickListener(presenter);
        adapter.setOnItemClickListener(presenter);
        // adapter.setOnLoadMoreListener(presenter, recyclerView);
        // GridLayoutManager mgr=new GridLayoutManager(getContext(),3);
        LinearLayoutManager mgr = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(mgr);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void check(BaseResponse baseResponse) {
       // BaseResponse baseResponse=gson.fromJson(json,BaseResponse.class);
        if (baseResponse!=null&&baseResponse.getCode()==200){
            if (baseResponse.getWorkType()==3){
                showMsg("删除成功");
                isEdited=true;
            }
           // setResult(0x512);
            //finish();

        }else {
            showMsg(baseResponse.getError());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void bindDataToView(GoodsRes goodsRes) {
        if (goodsRes.getCode() == 200 && goodsRes.getData() != null && goodsRes.getData().size() > 0) {
            if (goodsRes.getData()!=null&&goodsRes.getData().size()>0){

            }else {

            }
            adapter.getData().clear();
            adapter.getData().addAll(goodsRes.getData());
            adapter.notifyDataSetChanged();
            refreshComplite();
            hideNerError();
            buttonSend.setVisibility(View.VISIBLE);
            return;
        }
        showNetError();
        refreshNoData();
//        buttonSend.setVisibility(View.GONE);
        if (goodsRes.getCode() != 200)
            showMsg(goodsRes.getError());
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        GoodsInfo goodsInfo= (GoodsInfo) adapter.getData().get(position);
        switch (view.getId()){
            case R.id.edit_1:
                cdialog=InputDialog.getInstance(this,this,goodsInfo ,"修改提单号","提单号：",1);
                cdialog.show();
                break;
            case R.id.edit_2:
                cdialog=InputDialog.getInstance(this,this, goodsInfo,"修改货名","货名：",2);
                cdialog.show();
                break;
            case R.id.edit_3:
                cdialog=InputDialog.getInstance(this,this, goodsInfo,"修改件/毛/体","件：",3);
                cdialog.show();
                break;
            case R.id.edit_4:
                cdialog=InputDialog.getInstance(this,this, goodsInfo,"修改件/毛/体","毛：",4);
                cdialog.show();
                break;
            case R.id.edit_5:
                cdialog=InputDialog.getInstance(this,this, goodsInfo,"修改件/毛/体","体：",5);
                cdialog.show();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==0x512){
            isEdited=true;
            presenter.onRefresh();
        }
    }
}
